/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside
{
    /// <summary>
    /// 
    /// </summary>
    public partial class PageKioskToAutoGratuityPairingImp : PageImpBaseClass
    {
        ListView listviewAvailableGratuity;
        ListView listviewSelectedGratuity;

        ComboBox comboBoxName;
        Button buttonDelete;
        Button buttonNew;

        /// <summary>
        /// Constructor
        /// </summary>
        public PageKioskToAutoGratuityPairingImp(
            ListView listviewAvailableGratuity,
            ListView listviewSelectedGratuity,
            ComboBox comboBoxName,
            Button buttonDelete,
            Button buttonNew)
        {
            this.ButtonNumber = "17";
            this.ButtonLabelText = "KIOSK  Gratuity Map"; ///Extra spaces are there for alignment

            mNewGroupIsDisabled = true;
            mDeleteGroupIsDisabled = true;
            mDeleteEntityIsDisabled = true;
            mNewEntityIsDisabled = true;

            mButtonNewEntityName = "New Pairing";
            mButtonDeleteEntityName = "Delete Pairing";

            this.listviewAvailableGratuity = listviewAvailableGratuity;
            this.listviewSelectedGratuity = listviewSelectedGratuity;
            this.comboBoxName = comboBoxName;
            this.buttonDelete = buttonDelete;
            this.buttonNew = buttonNew;

            this.buttonDelete.Click += new RoutedEventHandler(buttonDelete_Click);
            this.buttonNew.Click += new RoutedEventHandler(buttonJoin_Click);

            ///Register the controls and lock the form.
            RegisterListview(listviewAvailableGratuity);
            RegisterListview(listviewSelectedGratuity);

            ///Add the combo boxes.
            ListofComboboxes.Add(comboBoxName);

            listviewSelectedGratuity.SelectionChanged += new SelectionChangedEventHandler(listview_SelectionChanged_NOTHROW);
            listviewAvailableGratuity.SelectionChanged += new SelectionChangedEventHandler(listview_SelectionChanged_NOTHROW);

            ///If the selection changes lock the form down.
            comboBoxName.SelectionChanged += new SelectionChangedEventHandler(comboBoxName_SelectionChanged_NOTHROW);

            mListOfControlsToHideInNonEditMode.Add(buttonDelete);
            mListOfControlsToHideInNonEditMode.Add(buttonNew);


            mEntityAddDeleteLoggingEnabled = true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        override public List<EntityObject> GetEntityObjectSet()
        {
            try
            {
                var q = from KIOSK in Context.KIOSKS.Include("AUTO_GRATUITYS")
                        where KIOSK.IsDeleted == false
                        select KIOSK;

                if (q.Any())
                {
                    return new List<EntityObject>(q.ToList());
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
            return null;
        }

        private ObservableCollection<AUTO_GRATUITY> GetSelectedAutoGratuitys()
        {
            ObservableCollection<AUTO_GRATUITY> selectedList = null;
            try
            {
                KIOSK kiosk = comboBoxName.SelectedItem as KIOSK;
                if (kiosk != null)
                {
                    kiosk.AUTO_GRATUITYS.Load();
                    foreach (AUTO_GRATUITY ag in kiosk.AUTO_GRATUITYS)
                    {
                        selectedList.Add(ag);
                    }
                }
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_PLACEHOLDER, exc);
            }
            return selectedList;
        }

        private void UpdateListViews()
        {
            try
            {
                KIOSK selectedKiosk = comboBoxName.SelectedItem as KIOSK;
                if (selectedKiosk != null)
                {
                    selectedKiosk.AUTO_GRATUITYS.Load();
                    var q1 = from AG in selectedKiosk.AUTO_GRATUITYS where AG.IsDeleted == false select AG;
                    if (q1.Any())
                    {
                        listviewSelectedGratuity.ItemsSource = q1.ToList();
                    }
                    else
                    {
                        listviewSelectedGratuity.ItemsSource = null;
                    }


                    ///The complex query below will find all auto-gratuity objects that have not
                    ///already been assigned to the currently selected KIOSK.
                    var q2 = from AG in Context.AUTO_GRATUITYS 
                             where AG.IsDeleted == false
                             select AG;

                    if (q2.Any())
                    {
                        List<AUTO_GRATUITY> agList = q2.ToList();
                        foreach (AUTO_GRATUITY ag in q2)
                        {
                            if (selectedKiosk.AUTO_GRATUITYS.Contains(ag))
                            {
                                agList.Remove(ag);
                            }
                        }


                        listviewAvailableGratuity.ItemsSource = agList;
                    }
                    else
                    {
                        listviewAvailableGratuity.ItemsSource = null;
                    }
                }
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_PLACEHOLDER, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void comboBoxName_SelectionChanged_NOTHROW(object sender, SelectionChangedEventArgs e)
        {
            UpdateListViews();
        }

        /// <summary>
        /// 
        /// </summary>
        public override void ResetFocus()
        {
            base.ResetFocus();
            buttonDelete.IsEnabled = false;
            buttonNew.IsEnabled = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void listview_SelectionChanged_NOTHROW(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ListView lv = sender as ListView;
                if (lv == listviewAvailableGratuity)
                {
                    listviewSelectedGratuity.SelectedItem = null;
                    buttonNew.IsEnabled = true;
                    buttonDelete.IsEnabled = false;
                }
                else
                {
                    listviewAvailableGratuity.SelectedItem = null;
                    buttonNew.IsEnabled = false;
                    buttonDelete.IsEnabled = true;
                }
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_PLACEHOLDER, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonJoin_Click(object sender, RoutedEventArgs e)
        {
            int count = listviewAvailableGratuity.SelectedItems.Count;
            KIOSK kiosk = comboBoxName.SelectedItem as KIOSK;
            if (count != 0 && listviewAvailableGratuity.SelectedItems.Count != 0 && kiosk != null)
            {
                kiosk.CreateAutoSeats = true;
                foreach (AUTO_GRATUITY ag in listviewAvailableGratuity.SelectedItems)
                {
                    kiosk.AUTO_GRATUITYS.Add(ag);
                }
                Context.SaveChanges();
                UpdateListViews();
                listviewSelectedGratuity.SelectedItem = null;
                listviewAvailableGratuity.SelectedItem = null;

                buttonNew.IsEnabled = false;
                buttonDelete.IsEnabled = false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override EntityObject GetSelectedEntityObject()
        {
            KIOSK selected = comboBoxName.SelectedItem as KIOSK;
            return selected;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        public override void UpdateUsersEntitySource()
        {
            ///Create the query object.
            var query = from KIOSK in Context.KIOSKS.Include("AUTO_GRATUITYS")
                        where KIOSK.IsDeleted == false
                        orderby KIOSK.Name
                        select KIOSK;

            if (query.Any())
            {
                comboBoxName.ItemsSource = query;
                comboBoxName.SelectedItem = query.First();
            }
            else
            {
                comboBoxName.ItemsSource = null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ComboBox GetEntityComboBox()
        {
            return comboBoxName;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            KIOSK kiosk = comboBoxName.SelectedItem as KIOSK;
            if (kiosk != null)
            {
                IList joinList = listviewSelectedGratuity.SelectedItems;
                foreach (AUTO_GRATUITY selected in joinList)
                {
                    kiosk.AUTO_GRATUITYS.Remove(selected);
                }

                Context.SaveChanges();
                UpdateListViews();

                buttonNew.IsEnabled = false;
                buttonDelete.IsEnabled = false;
            }
        }
    }
}
